package com.yanqu.road.dao.impl.activity.sonhai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiPatronsData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiTeamData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SonHaiTeamDataDaoImpl extends TempDataObjectDao<SonHaiTeamData> {

    public static String getSonHaiTeamDataSql = "select * from `t_c_activity_son_hai_team_data` where `activityId`= ? and groupId = ?";
    public Map<Integer, SonHaiTeamData> getSonHaiTeamDataMap(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, SonHaiTeamData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSonHaiTeamDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                SonHaiTeamData data = parse(rs);
                map.put(data.getTeamId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateSonHaiTeamDataSql = "update `t_c_activity_son_hai_team_data` set leaderId=?,maxRegionId=?,createTime=? where activityId=? and groupId = ? and teamId = ?";

    @Override
    protected boolean updateData(SonHaiTeamData sonHaiTeamData) {
        sonHaiTeamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiTeamData.getLeaderId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiTeamData.getMaxRegionId()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiTeamData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiTeamData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiTeamData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiTeamData.getTeamId()));
        if (getDb().executeNoneQuery(updateSonHaiTeamDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSonHaiTeamDataSql = "insert into `t_c_activity_son_hai_team_data`(activityId,groupId,teamId,leaderId,maxRegionId,createTime) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(SonHaiTeamData sonHaiTeamData) {
        sonHaiTeamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiTeamData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiTeamData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiTeamData.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiTeamData.getLeaderId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiTeamData.getMaxRegionId()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiTeamData.getCreateTime()));
        if (getDb().executeNoneQuery(insertSonHaiTeamDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SonHaiTeamData parseData(ResultSet rs) throws SQLException {
        SonHaiTeamData sonHaiTeamData = new SonHaiTeamData();
        sonHaiTeamData.setActivityId(rs.getInt("activityId"));
        sonHaiTeamData.setGroupId(rs.getInt("groupId"));
        sonHaiTeamData.setTeamId(rs.getInt("teamId"));
        sonHaiTeamData.setLeaderId(rs.getLong("leaderId"));
        sonHaiTeamData.setMaxRegionId(rs.getInt("maxRegionId"));
        sonHaiTeamData.setCreateTime(rs.getLong("createTime"));
        return sonHaiTeamData;
    }
}
